package com.shou.domain.reserve.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shou.domain.common.AbstractService;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.reserve.model.ReservationEntity;
import com.shou.domain.reserve.repository.IReservationRepository;
import com.shou.domain.reserve.service.IReservationService;
import com.shou.types.common.TableNameEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Nemo
* @description 针对表【reservations(预定表，用于管理车位的预定情况)】的数据库操作Service实现
* @createDate 2025-02-07 22:10:51
*/
@Service
public class ReservationService extends AbstractService implements IReservationService {
    @Resource
    private IReservationRepository reservationRepository;

    @Override
    public Integer insertReservation(ReservationEntity reservation) {
        validateReservationEntity(reservation);
        return reservationRepository.insert(reservation);
    }

    @Override
    public ReservationEntity getReservationById(Integer id) {
        return reservationRepository.getReservationById(id);
    }

    @Override
    public IPage<ReservationEntity> pageQuery(PageQueryEntity pageQueryEntity) {
        pageQueryEntity.setSortName(camelToSnake(pageQueryEntity.getSortName()));
        validateQueryParams(pageQueryEntity, TableNameEnum.RESERVATION);
        return reservationRepository.getReservationsPage(pageQueryEntity);
    }

    @Override
    public void updateReservationById(ReservationEntity reservationEntity) {
        Integer id = reservationEntity.getId();
        if(id == null || !checkIfExistById(id, TableNameEnum.RESERVATION)){
            throw new RuntimeException("id为" + id + "的记录不存在");
        }
        validateReservationEntity(reservationEntity, reservationEntity.getId());
        reservationRepository.updateReservationById(reservationEntity);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        super.deleteByIds(ids, TableNameEnum.RESERVATION);
    }

    @Override
    public void deleteAll() {
        super.deleteAll(TableNameEnum.RESERVATION);
    }

    private void validateReservationEntity(ReservationEntity reservation, Integer... excludeIds) {
        validateByReservedUserId(reservation.getUserId(), TableNameEnum.RESERVATION.getEnTableName(), excludeIds);
        validateBySpaceCode(reservation.getSpaceCode(), TableNameEnum.PARKING_SPACE.getEnTableName(), excludeIds);
    }
}




